package cn.xshi.scms.web;

import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.util.StringUtil;
import cn.xshi.scms.form.SCMSMonitorForm;
import cn.xshi.scms.model.SCMSMonitor;
import cn.xshi.scms.service.SCMSMonitorService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 监控主表
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/scms/monitor")
@Api(value = "服务器监控主表",tags = "服务器监控主表",description = "服务器监控主表")
public class SCMSMonitorController extends BaseAction {

    @Resource
    SCMSMonitorService scmsMonitorService;

    /**
     * 查询物理机监控列表并分页
     * @param scmsMonitorForm
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value="查询物理机监控列表并分页", notes="查询物理机监控列表并分页")
    @GetMapping(value="/list")
    @Auth(value = "/scms/monitor/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BasePage<List<SCMSMonitor>> getSCMSMonitorListByCondition(SCMSMonitorForm scmsMonitorForm, Integer pageNo, Integer pageSize){
        commonHPagerByPageNo(pageNo,pageSize);
        List<SCMSMonitor> scmsMonitorList = scmsMonitorService.getSCMSMonitorListByCondition(scmsMonitorForm);
        PageInfo<SCMSMonitor> page = new PageInfo<SCMSMonitor>(scmsMonitorList);
        return outPageBootByPageNo(page,pageNo,pageSize);
    }

    /**
     * 查询单个服务器主信息
     * @param id
     */
    @ApiOperation(value="查询单个服务器主信息", notes="查询单个服务器主信息")
    @GetMapping(value="/get/{id}")
    @Auth(value = "/scms/monitor/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<SCMSMonitor> getSCMSMonitorById(@PathVariable("id")String id){
        SCMSMonitor scmsMonitor = scmsMonitorService.getSCMSMonitorById(id);
        return outDataStr(scmsMonitor);
    }

    /**
     * 根据Mac查询单个服务器主信息
     * @param id
     */
    @ApiOperation(value="根据Mac查询单个服务器主信息", notes="根据Mac查询单个服务器主信息")
    @GetMapping(value="/mac/{id}")
    @Auth(value = "/scms/monitor/mac",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<SCMSMonitor> getSCMSMonitorByMac(@PathVariable("id")String id){
        SCMSMonitor scmsMonitor = scmsMonitorService.getSCMSMonitorByMac(id);
        return outDataStr(scmsMonitor);
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping(value="/delete")
    @ApiOperation(value="删除", notes="删除")
    public BaseResult delSCMSMonitor(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=scmsMonitorService.delSCMSMonitor(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 接收上报监控服务器信息
     * @param scmsMonitor
     */
    @ApiOperation(value="接收上报监控服务器信息", notes="接收上报监控服务器信息")
    @PostMapping(value="/save")
    @Auth(value = "/scms/monitor/save",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
    public BaseResult saveSCMSMonitor(@RequestBody SCMSMonitor scmsMonitor){
        int i = 0;
        if(null != scmsMonitor){
            scmsMonitorService.receiveSCMSMonitor(scmsMonitor);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

//    @Bean(name = "/hessian")
//    public BaseResult hessian(@RequestBody SCMSMonitor scmsMonitor){
//        int i = 0;
//        if(null != scmsMonitor){
//            scmsMonitorService.saveSCMSMonitor(scmsMonitor);
//        }
//        if(i>0){
//            return outAudStr(true);
//        }else{
//            return outAudStr(false);
//        }
//    }
}

